﻿
<!--
https://github.com/netnr
https://gitee.com/netnr
https://www.netnr.com
https://zme.ink
-->


<!DOCTYPE html>
<html>
<head>

    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <link rel='shortcut icon' href='/favicon.ico' type='image/x-icon' />


    <title>人民币转大写 脚本服务</title>

    <meta name="keywords" content="netnr NET牛人 人民币转大写" />
    <meta name="description" content="Script Services 人民币转大写" />

</head>
<body>
<div id='LoadingMask' style='position:fixed;top:0;left:0;bottom:0;right:0;background-color:white;z-index:19999;background-image:url('/images/loading.svg');background-repeat:no-repeat;background-position:48% 45%'></div>
<link href='https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css' rel='stylesheet'>
<link href='https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css' rel='stylesheet'>
<script src='https://cdn.jsdelivr.net/npm/jquery@3.5.0/dist/jquery.min.js'></script>
<script src='https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js'></script>
<script src='https://cdn.jsdelivr.net/npm/jzjs@2.0.2/2.0.2/jz.min.js'></script>
<link href='https://cdn.jsdelivr.net/npm/netnrnav@1.1.0/src/netnrnav.css' rel='stylesheet' />
<script src='https://cdn.jsdelivr.net/npm/netnrnav@1.1.0/src/netnrnav.js' defer async></script>

<script>
    $.ajax({
        url: '/images/icon.svg?20201228',
        dataType: 'text',
        success: function (data) {
            $('body').append('<div class="d-none">' + data + '</div>')
        }
    })
</script>

        <link href="/css/global.css?v=YuzFOH_Z7ma5bs-3HMdZQu0aPQZ1pkBqrzQ_Ri8Yh3k" rel="stylesheet" />
        <script src="/js/global.js?v=ckkNo3hER74SKnNCuouMgEfxEpEMP3phtPl9uA0_Lnc"></script>
            <!--导航-->
            <button type="button" class="MenuToggle show-in-mobile btn btn-primary ml-3 mt-3">
                <span class="fa fa-align-justify fa-2x"></span>
            </button>
            <div class="netnrnav horizontal">
                <div class="netnrnav-wrapper">
                    <ul>
                        <li><a href="https://www.netnr.com" class="siteIcon"><img src="/favicon.svg" /> 首页</a></li>
                        <li><a href="https://www.netnr.com/gist/discover" title="代码片段"><span class="fa fa-fw fa-file-code-o"></span> Gist</a></li>
                        <li><a href="https://www.netnr.com/run/discover" title="在线运行代码"><span class="fa fa-fw fa-play"></span> Run</a></li>
                        <li><a href="https://www.netnr.com/doc" title="文档管理"><span class="fa fa-fw fa-book"></span> Doc</a></li>
                        <li><a href="https://www.netnr.com/draw/discover" title="绘制"><span class="fa fa-fw fa-paint-brush"></span> Draw</a></li>
                        <li><a href="/" title="ss.js.org"><span class="fa fa-fw fa-wrench"></span> 服务</a></li>
                        <li style="background-image: radial-gradient(ellipse, white, rgb(227, 248, 240))">
                            <!--标题-->
                                <a href='javascript:location.reload(false)' title="人民币转大写">
                                    <svg class="titleicon"><use xlink:href="#rmb"></use></svg> &#x4EBA;&#x6C11;&#x5E01;&#x8F6C;&#x5927;&#x5199;
                                </a>
                        </li>
                        <li>
                            <a class="text-muted"><span class="fa fa-fw fa-ellipsis-h"></span></a>
                            <ul>
                                <li><a target="_blank" title="码云" href="https://gitee.com/netnr"><i class="fa fa-fw fa-git"></i> Gitee <sup><i class="fa small fa-external-link"></i></sup></a></li>
                                <li><a target="_blank" title="GitHub" href="https://github.com/netnr"><i class="fa fa-fw fa-github"></i> GitHub <sup><i class="fa small fa-external-link"></i></sup></a></li>
                            </ul>
                        </li>
                    </ul>
                </div>
            </div>
        <div class="min300 mt-3">
            <div class="container-fluid">
    <div class="row">
        <div class="col-md-12">
            <input class="form-control form-control-lg fa-3x" placeholder="请输入金额 ￥" id="txtMoney" autofocus />
        </div>
        <div class="col-md-12 my-3">
            <input class="form-control form-control-lg fa-3x" id="txtCapital" />
        </div>
    </div>
</div>

<script>
    //by https://github.com/michaellyu/rmb-x

    function rmb(value) {
        if (Object.prototype.toString.call(value) === '[object Number]' && value >= 0.01) {
            const numbers = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
            const leftUnits = ['元', '拾', '佰', '仟', '万', '拾', '佰', '仟', '亿', '拾', '佰', '仟', '万', '拾', '佰', '仟', '万'];
            const rightUnits = ['角', '分'];

            const fragment = [];
            const [leftValues, rightValues] = String(value)
                .split('.')
                .map(part => part.split('').map(i => i * 1));

            const leftValueLength = leftValues.length; // 整数部分位数
            const unit1 = leftValueLength - 1; // 元位
            const unit5 = leftValueLength - 5; // 万位
            const unit9 = leftValueLength - 9; // 亿位
            const unit13 = leftValueLength - 13; // 万亿位
            const unit17 = leftValueLength - 17; // 万万亿位
            const hasLeftValue = leftValueLength > 1 || leftValues[0] > 0; // 整数部分不为0
            const hasRightValue = rightValues && (rightValues[0] > 0 || rightValues[1] > 0); // 小数部分不为0
            const has678Value = leftValues[unit5 - 1] > 0 || leftValues[unit5 - 2] > 0 || leftValues[unit5 - 3] > 0; // 拾万、佰万或仟万位不为0
            const overflowIndex = leftValueLength - leftUnits.length; // 溢出位索引

            let leftUnitIndex = 0;
            for (let i = leftValueLength - 1; i >= 0; i--) {
                if (
                    (leftValues[i] > 0)
                    || (i === unit1 && hasLeftValue)
                    || (i === unit5 && has678Value)
                    || i === unit9
                    || i === unit13
                    || i === unit17
                ) { // 元、万、亿、万亿、万万亿或当前位不为0
                    fragment.unshift(leftUnits[leftUnitIndex]);
                }

                if (
                    leftValues[i] > 0
                    || (leftValues[i + 1] > 0 && i !== unit5 && i !== unit9 && i !== unit13 && i !== unit17)
                    || i <= overflowIndex
                ) { // 当前位不为0，或低一位不为0且当前位非万、亿、万亿、万万亿，或当前为溢出位
                    fragment.unshift(numbers[leftValues[i]]);
                }

                leftUnitIndex++;
            }

            if (hasRightValue) {
                // 角
                if (rightValues[0] > 0 || hasLeftValue) { // 角位不为0，或整数位不为0
                    fragment.push(numbers[rightValues[0]]);
                }
                if (rightValues[0] > 0) { // 角位不为0
                    fragment.push(rightUnits[0]);
                }
                // 分
                if (rightValues[1] > 0) {
                    fragment.push(numbers[rightValues[1]]);
                    fragment.push(rightUnits[1]);
                }
            } else { // 没有小数位
                fragment.push('整');
            }
            return fragment.join('');
        }
        return '零元整';
    }

    $('#txtMoney').on('input', function () {
        $('#txtCapital').val(rmb(Number(this.value.trim())))
    })
</script>
        </div>
        <a href="javascript:ss.toTop();" class="totop" title="返回顶部">ˆ</a>
</body>
</html>
